package com.ziyucoding.springboot.labs.largefileupload.model.result;

import com.ziyucoding.springboot.labs.largefileupload.exception.IErrorCode;
import com.ziyucoding.springboot.labs.largefileupload.exception.JsonException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.time.Instant;
import java.time.ZonedDateTime;

/**
 * 返回结果
 *
 * @author ziyucoding
 * @date 2023-06-02
 **/
@AllArgsConstructor
@Setter
@Getter
@ToString
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * T <>
     */
    private T data;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * code 错误码
     */
    private Integer code;

    /**
     * url 请求路径
     */
    private String url;

    /**
     * errorStackTrack 返回错误信息堆栈
     */
    private String errorStackTrack;

    /**
     * 请求结果生成的时间戳
     */
    private Instant time;

    public Result() {
        this.time = ZonedDateTime.now().toInstant();
    }

    public Result(boolean success, T data, String msg, Integer code) {
        this.success = success;
        this.data = data;
        this.msg = msg;
        this.code = code;
        this.time = ZonedDateTime.now().toInstant();
    }

    public Result(IResultCode resultType) {
        this.code = resultType.getCode();
        this.msg = resultType.getMsg();
        this.time = ZonedDateTime.now().toInstant();
    }

    public Result(boolean success, String message, Integer code) {
        Result.of(success, null, message, code);
    }

    public Result(boolean success, IResultCode resultType) {
        Result.of(success, null, resultType.getMsg(), resultType.getCode());
    }

    public Result(boolean success, IResultCode resultType, T data) {
        Result.of(success, data, resultType.getMsg(), resultType.getCode());
    }

    public static <T> Result<T> of(boolean success, T data, String message, Integer code) {
        return new Result<T>(success, data, message, code);
    }

    public static <T> Result<T> ok() {
        return Result.ok(ResultStatus.SUCCESS);
    }

    public static <T> Result<T> ok(T data) {
        return Result.of(true, data,
                ResultStatus.SUCCESS.getMsg(), ResultStatus.SUCCESS.getCode());
    }

    public static <T> Result<T> ok(IResultCode resultEnum) {
        return Result.of(true, null, resultEnum.getMsg(),
                resultEnum.getCode());
    }

    public static <T> Result<T> ok(String message) {
        return Result.of(true, null, message,
                ResultStatus.DEFAULT_SUCCESS_CODE);
    }

    public static <T> Result<T> ok(String message, Integer code) {
        return Result.of(true, null, message, code);
    }

    public static <T> Result<T> ok(T data, String message) {
        return Result.of(true, data, message,
                ResultStatus.DEFAULT_SUCCESS_CODE);
    }

    public static <T> Result<T> fail(T data) {
        return Result.of(false, data, ResultStatus.FAIL.getMsg(), ResultStatus.FAIL.getCode());
    }

    public static <T> Result<T> fail(IResultCode resultCode) {
        return Result.of(false, null, resultCode.getMsg(), resultCode.getCode());
    }

    public static <T> Result<T> fail(IResultCode resultCode, T data) {
        return Result.of(false, data, resultCode.getMsg(), resultCode.getCode());
    }

    public static <T> Result<T> fail(String message) {
        return Result.of(false, null, message, ResultStatus.DEFAULT_FAILURE_CODE);
    }

    public static <T> Result<T> fail(String message, Integer errorcode) {
        return Result.of(false, null, message, errorcode);
    }

    public static <T> Result<T> fail() {
        return Result.fail(ResultStatus.FAIL);
    }

    public static <T> Result<T> fail(T data, String message, Integer errorcode) {
        return Result.of(false, data, message, errorcode);
    }

    public static void throwFail(IErrorCode errorCode) {
        throw new JsonException(errorCode);
    }

    public static void throwFail(String message) {
        throw new JsonException(message);
    }

    public static void throwFail(Integer code, String message) {
        throw new JsonException(message);
    }

    public static <T extends JsonException> Result<T> ofException(T t) {
        return of(false, null, t.getMessage(), t.getCode());
    }

    public static <T extends JsonException> Result<T> ofException(T t, Integer code) {
        return of(false, null, t.getMessage(), code);
    }

}
